package com.huang.controller;


import cn.hutool.crypto.SecureUtil;
import com.github.pagehelper.PageInfo;
import com.huang.pojo.*;
import com.huang.service.AdminService;
import com.huang.service.StudentService;
import com.huang.service.TeacherService;
import com.huang.util.MailUtils;
import com.huang.util.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class AdminController {
    @Autowired
    @Qualifier("AdminServiceImpl")
    private AdminService adminService;
    @Autowired
    @Qualifier("TeacherServiceImpl")
    private TeacherService teacherService;
    @Autowired
    @Qualifier("StudentServiceImpl")
    private StudentService studentService;


    //管理员登陆
    @RequestMapping("/admin/goLogin")
    public String gologin() {
        return "adminLogin";
    }

    @RequestMapping("/admin/login")
    public String loginAdmin(Admin admin, Model model, HttpSession session) {
        //System.out.println("管理员名字"+admin.getAdminName());
        //System.out.println("管理员密码"+admin.getAdminPwd());

        //密码加密
        admin.setAdminPwd(SecureUtil.md5(admin.getAdminPwd()));

        Admin admin1 = adminService.loginAdmin(admin);
        if (admin1 == null) {
            model.addAttribute("adminLoginErrorMsg","账号或密码输入有误！");
            return "adminLogin";
        } else {
            model.addAttribute("admin1", admin);
            session.setAttribute("adminData", admin1);

            //加载头像
            UserAvatar userAvatar=new UserAvatar();
            userAvatar.setUserNo(admin.getAdminName());
            model.addAttribute("userAvatar",studentService.JudgeAvatar(userAvatar));

            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
            //管理员活动日志登陆时间记录
            UserDiary userDiary=new UserDiary();
            userDiary.setUserNo(admin1.getAdminName());
            Date now = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String time = dateFormat.format(now);
            userDiary.setUserLoginTime(time);
            if(studentService.stuLoginRecord(userDiary)!=0){
                System.out.println("登陆时间已成功记录"+time);
                session.setAttribute("adminLoginTime",time);
            }
            //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

            return "adminIndex";
        }
    }

    //管理员完善个人信息
    @RequestMapping("/admin/toImproveMsg")
    public String toImproveMsg(Admin admin, Model model, HttpSession session) {
        System.out.println("01-->" + admin);
        model.addAttribute("admin", session.getAttribute("adminData"));

        //加载头像
        UserAvatar userAvatar=new UserAvatar();
        userAvatar.setUserNo(admin.getAdminName());
        model.addAttribute("userAvatar",studentService.JudgeAvatar(userAvatar));

        return "adminImproveMsg";
    }

    @RequestMapping("/admin/improveMsg")
    public String improveMsg(Admin admin, Model model,HttpSession session) {
        System.out.println("002--->" + admin);
        if (adminService.improveAdmin(admin) != 0) {
            model.addAttribute("rightImproveMsg", "成功更新个人信息");

            //加载头像
            UserAvatar userAvatar=new UserAvatar();
            userAvatar.setUserNo(admin.getAdminName());
            model.addAttribute("userAvatar",studentService.JudgeAvatar(userAvatar));

            //***************************************************************************************************
            //添加管理员活动跟踪日志
            UserDiary userDiary=new UserDiary();
            userDiary.setUserOperate("完善信息");

            Date nowRecord = new Date();
            SimpleDateFormat dateFormatRecord = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String timeRecord = dateFormatRecord.format(nowRecord);
            userDiary.setUserOperateTime(timeRecord);

            Admin adminRecord= (Admin) session.getAttribute("adminData");
            userDiary.setUserNo(adminRecord.getAdminName());

            String teaLoginTime= (String) session.getAttribute("adminLoginTime");
            userDiary.setUserLoginTime(teaLoginTime);
            if(studentService.stuOperateRecord(userDiary)!=0){
                System.out.println("完善信息已被成功记录"+timeRecord);
            }
            //***************************************************************************************************

            return "adminImproveMsg";
        } else {
            model.addAttribute("errorImproveMsg", "未能成功更新个人信息");

            //加载头像
            UserAvatar userAvatar=new UserAvatar();
            userAvatar.setUserNo(admin.getAdminName());
            model.addAttribute("userAvatar",studentService.JudgeAvatar(userAvatar));

            return "adminImproveMsg";
        }
    }

    //管理员管理教师
    @RequestMapping("/admin/toManageTeacher")
    public String toManageTeacher(Model model) {
        List<Teacher> list = adminService.showAllTeacher();
        model.addAttribute("list", list);
        return "adminViewTeacher";
    }

    //管理员管理教师
    @RequestMapping("/admin/toManageTeacher01")
    public String toManageTeacher01(int page,Model model) {
        //调用分页的业务
        PageInfo<Teacher> pageInfo=adminService.showAllTeacherByPage(page,1);

        model.addAttribute("pageInfo", pageInfo);
        return "testAdminViewTeacher";
    }

    //管理员管理学生
    @RequestMapping("/admin/toManageStudent")
    public String toManageStudent(Model model) {
        List<Student> list = adminService.showAllStudent();
        model.addAttribute("list", list);
        return "adminViewStudent";
    }

    //管理员管理活动
    @RequestMapping("/admin/toManageActivity")
    public String toManageActivity(Model model) {
        List<Activity> list = adminService.showAllActivity();
        model.addAttribute("list", list);

        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String time = dateFormat.format(now);

        model.addAttribute("nowTime", time);

        return "adminViewActivity";
    }

    //管理员发布公告
    @RequestMapping("/admin/toAnnounce")
    public String toAnnounce(Admin admin, Model model) {
        model.addAttribute("admin", admin);
        return "adminAnnounce";
    }

    @RequestMapping("/admin/announce")
    public String announce(Announcement announcement, Model model,HttpSession session) {
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String time = dateFormat.format(now);
        announcement.setNoticeTime(time);
        announcement.setNoticeStatus("T");
        Admin admin= (Admin) session.getAttribute("adminData");
        announcement.setAdminName(admin.getAdminName());

        if (adminService.issueNotice(announcement) != 0) {

            //***************************************************************************************************
            //添加管理员活动跟踪日志
            UserDiary userDiary=new UserDiary();
            userDiary.setUserOperate("发布公告"+announcement.getAdminNotice());

            Date nowRecord = new Date();
            SimpleDateFormat dateFormatRecord = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String timeRecord = dateFormatRecord.format(nowRecord);
            userDiary.setUserOperateTime(timeRecord);

            Admin adminRecord= (Admin) session.getAttribute("adminData");
            userDiary.setUserNo(adminRecord.getAdminName());

            String teaLoginTime= (String) session.getAttribute("adminLoginTime");
            userDiary.setUserLoginTime(teaLoginTime);
            if(studentService.stuOperateRecord(userDiary)!=0){
                System.out.println("发布公告"+announcement.getAdminNotice()+"已被成功记录"+timeRecord);
            }
            //***************************************************************************************************

            model.addAttribute("rightAnnounce", "发布成功");
        } else {
            model.addAttribute("errorAnnounce", "发布失败");
        }
        return "adminAnnounce";
    }

    //管理员查看历史公告
    @RequestMapping("/admin/toHistoryNotice")
    public String toHistoryNotice(Model model) {
        List<Announcement> list = adminService.historyNotice();
        model.addAttribute("list", list);
        return "adminViewNotice";
    }

    //管理员查看自己发布的活动
    @RequestMapping("/admin/toMyNotice")
    public String toMyNotice(Admin admin, Model model, HttpSession session) {
        if (admin.getAdminName() == null) {
            admin = (Admin) session.getAttribute("adminData");
        }
        List<Announcement> list = adminService.myNotice(admin);
        model.addAttribute("list", list);
        return "adminViewSelfNotice";
    }

    //管理员使公告过期
    @RequestMapping("/admin/toExpiredNotice")
    public String toExpiredNotice(Announcement announcement, Model model,HttpSession session) {
        System.out.println("管理员使公告过期" + announcement);
        if (adminService.expiredNotice(announcement) != 0) {
            model.addAttribute("expiredRightMsg", "公告已成功失效");

            //***************************************************************************************************
            //添加管理员活动跟踪日志
            UserDiary userDiary=new UserDiary();
            userDiary.setUserOperate("使公告过期"+announcement.getAdminNotice());

            Date nowRecord = new Date();
            SimpleDateFormat dateFormatRecord = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String timeRecord = dateFormatRecord.format(nowRecord);
            userDiary.setUserOperateTime(timeRecord);

            Admin adminRecord= (Admin) session.getAttribute("adminData");
            userDiary.setUserNo(adminRecord.getAdminName());

            String teaLoginTime= (String) session.getAttribute("adminLoginTime");
            userDiary.setUserLoginTime(teaLoginTime);
            if(studentService.stuOperateRecord(userDiary)!=0){
                System.out.println("使公告过期"+announcement.getAdminNotice()+"已被成功记录"+timeRecord);
            }
            //***************************************************************************************************

            return "redirect:/admin/toMyNotice";
        } else {
            model.addAttribute("expiredErrorMsg", "操作失败，请检查后台");
            return "redirect:/admin/toMyNotice";
        }
    }

    ;

    //管理员查看公告的用户反馈
    @RequestMapping("/admin/toUserFeedBack")
    public String toUserFeedBack(Announcement announcement, Model model) {
        List<FeedBack> list = adminService.showUserFeedBack(announcement);
        model.addAttribute("list", list);
        return "adminViewFeedBack";
    }

    //管理员查看反馈人员信息(学生)
    @RequestMapping("/admin/toViewFeedBackStu")
    public String toViewFeedBackStu(Student student, Model model) {
        List<Student> list = adminService.findStuByNo(student);
        model.addAttribute("list", list);
        return "adminViewStudent";
    }

    //管理员查看反馈人员信息(教师)
    @RequestMapping("/admin/toViewFeedBackTea")
    public String toViewFeedBackTea(Teacher teacher, Model model) {
        List<Teacher> list = adminService.findTeaByNo(teacher);
        model.addAttribute("list", list);
        return "adminViewTeacher";
    }

    //查看某个老师发布过的活动
    @RequestMapping("/admin/toTeaActivity")
    public String toTeaActivity(Teacher teacher, Model model) {
        List<Activity> list = adminService.findActivityByTea(teacher);
        model.addAttribute("list", list);

        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String time = dateFormat.format(now);
        model.addAttribute("nowTime", time);

        return "adminViewActivity";
    }

    //管理员查看参加某个活动的学生
    @RequestMapping("/admin/toViewActivityStu")
    public String toViewActivityStu(Activity activity, Model model, HttpSession session) {
        if(session.getAttribute("teacher")!=null){
            model.addAttribute("Identity", "teacher");
        }else {
            model.addAttribute("Identity", "admin");
        }
        List<ActivityPeople> list = teacherService.queryActivityStudent2(activity);
        model.addAttribute("list", list);
        return "teacherManageActivityStudent";
    }

    //管理员查看某个学生参加过的活动
    @RequestMapping("/admin/toViewStuActivity")
    public String toViewStuActivity(ActivityPeople activityPeople, Model model) {
        model.addAttribute("Identity", "admin");
        List<Activity> list = studentService.myActivity(activityPeople);
        model.addAttribute("list", list);

        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String time = dateFormat.format(now);
        model.addAttribute("nowTime", time);

        return "studentActivity";
    }

    //管理员添加老师
    @RequestMapping("/admin/toAddTeacher")
    public String toAddTeacher() {
        return "adminAddTeacher";
    }

    @RequestMapping("/admin/addTeacher")
    public String addTeacher(Teacher teacher, Model model, HttpSession session) {
        //密码加密
        teacher.setTeacherPwd(SecureUtil.md5(teacher.getTeacherNo()));

        System.out.println("管理员添加老师" + teacher);
        if (!adminService.findTeaByNo(teacher).isEmpty()) {
            System.out.println("管理员添加老师" + teacher);
            model.addAttribute("addTeacherErrorMsg", "添加失败,此工号已被注册过");
            return "adminAddTeacher";
        }
        Admin admin = (Admin) session.getAttribute("adminData");
        teacher.setAdminName(admin.getAdminName());

        //teacher.setTeacherPwd(teacher.getTeacherNo());

        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String time = dateFormat.format(now);
        teacher.setTeacherAddTime(time);

        teacher.setTeacherStatus("F");

        teacher.setTeacherCode(UuidUtil.getUuid());

        if (adminService.addTeacher(teacher) != 0) {
            //发送激活邮件
            String content = "<a href='http://localhost:8080/teacher/toActivation?teacherCode=" + teacher.getTeacherCode() + "'>确认激活学生素质拓展管理系统</a>" +
                    "<br>或者访问http://localhost:8080/teacher/toActivation?teacherCode=" + teacher.getTeacherCode();
            MailUtils.sendMail(teacher.getTeacherEmail(), content, "激活邮件");
            model.addAttribute("addTeacherRightMsg", "添加成功，等待老师邮箱验证激活");

            //***************************************************************************************************
            //添加管理员活动跟踪日志
            UserDiary userDiary=new UserDiary();
            userDiary.setUserOperate("添加老师"+teacher.getTeacherNo());

            Date nowRecord = new Date();
            SimpleDateFormat dateFormatRecord = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String timeRecord = dateFormatRecord.format(nowRecord);
            userDiary.setUserOperateTime(timeRecord);

            Admin adminRecord= (Admin) session.getAttribute("adminData");
            userDiary.setUserNo(adminRecord.getAdminName());

            String teaLoginTime= (String) session.getAttribute("adminLoginTime");
            userDiary.setUserLoginTime(teaLoginTime);
            if(studentService.stuOperateRecord(userDiary)!=0){
                System.out.println("添加老师"+teacher.getTeacherNo()+"已被成功记录"+timeRecord);
            }
            //***************************************************************************************************

            return "adminAddTeacher";
        } else {
            model.addAttribute("addTeacherErrorMsg", "添加失败");
            return "adminAddTeacher";
        }
    }

    //管理员查看自己注册过的老师
    @RequestMapping("/admin/toSelfTeacher")
    public String toSelfTeacher(Admin admin, Model model, HttpSession session) {
        if (admin.getAdminName() == null) {
            admin = (Admin) session.getAttribute("adminData");
        }
        List<Teacher> list = adminService.showSelfTea(admin);
        model.addAttribute("list", list);
        model.addAttribute("Identity", "admin");
        return "adminViewTeacher";
    }

    //管理员注销自己的注册的教师
    @RequestMapping("/admin/toRemoveSelfTea")
    public String toRemoveSelfTea(Teacher teacher, Model model,HttpSession session) {
        if (adminService.removeSelfTea(teacher) != 0) {
            model.addAttribute("removeRightMsg", "操作完成，已成功注销");

            //***************************************************************************************************
            //添加管理员活动跟踪日志
            UserDiary userDiary=new UserDiary();
            userDiary.setUserOperate("注销老师"+teacher.getTeacherNo());

            Date nowRecord = new Date();
            SimpleDateFormat dateFormatRecord = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
            String timeRecord = dateFormatRecord.format(nowRecord);
            userDiary.setUserOperateTime(timeRecord);

            Admin adminRecord= (Admin) session.getAttribute("adminData");
            userDiary.setUserNo(adminRecord.getAdminName());

            String teaLoginTime= (String) session.getAttribute("adminLoginTime");
            userDiary.setUserLoginTime(teaLoginTime);
            if(studentService.stuOperateRecord(userDiary)!=0){
                System.out.println("注销老师"+teacher.getTeacherNo()+"已被成功记录"+timeRecord);
            }
            //***************************************************************************************************

            return "redirect:/admin/toSelfTeacher";
        } else {
            model.addAttribute("removeErrorMsg", "操作失败，请重试");
            return "redirect:/admin/toSelfTeacher";
        }
    }

    //管理员查看学生的学分
    @RequestMapping("/admin/toShowStuScore")
    public String toShowStuScore(Student student, Model model) {
        List<StudentScore> list = studentService.showSelfScore(student);
        model.addAttribute("list", list);
        model.addAttribute("studentNo",student.getStudentNo());
        float sumScore = 0;
        for (StudentScore studentScore : list) {
            sumScore = sumScore + studentScore.getActivityScore();
        }
        model.addAttribute("sumScore", sumScore);
        return "studentSelfScore";
    }

    //管理员查看活动详情
    @RequestMapping("/admin/toViewActivityDescription")
    public String toViewActivityDescription(Activity activity,Model model) {
        ActivityDescription activityDescription=teacherService.findActivityDescriptionByNo(activity);
        model.addAttribute("activityDescription",activityDescription);
        return "adminViewActivityDetail";
    }

    //管理员查看个人信息
    @RequestMapping("/admin/toViewSelfInfo")
    public String toViewSelfInfo(Admin admin,Model model){
        admin=adminService.findAdminByName(admin);
        model.addAttribute("admin",admin);
        return "adminViewSelfInfo";

    }

    //管理员生成微信名片
    @RequestMapping("/admin/toGenerateInfoCard")
    public String toGenerateInfoCard(Admin admin,Model model,HttpSession session){
        admin=adminService.findAdminByName(admin);
        model.addAttribute("admin",admin);

        //***************************************************************************************************
        //添加管理员活动跟踪日志
        UserDiary userDiary=new UserDiary();
        userDiary.setUserOperate("生成微信名片");

        Date nowRecord = new Date();
        SimpleDateFormat dateFormatRecord = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String timeRecord = dateFormatRecord.format(nowRecord);
        userDiary.setUserOperateTime(timeRecord);

        Admin adminRecord= (Admin) session.getAttribute("adminData");
        userDiary.setUserNo(adminRecord.getAdminName());

        String teaLoginTime= (String) session.getAttribute("adminLoginTime");
        userDiary.setUserLoginTime(teaLoginTime);
        if(studentService.stuOperateRecord(userDiary)!=0){
            System.out.println("生成微信名片已被成功记录"+timeRecord);
        }
        //***************************************************************************************************

        return "adminGenerateInfoCard";
    }

    //管理员退出登录
    @RequestMapping("/admin/toLogout")
    public String toLogout(HttpSession session){

        Admin adminRecord= (Admin) session.getAttribute("adminData");
        UserDiary userDiary=new UserDiary();
        userDiary.setUserNo(adminRecord.getAdminName());
        String adminLoginTime= (String) session.getAttribute("adminLoginTime");
        userDiary.setUserLoginTime(adminLoginTime);
        Date now = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//可以方便地修改日期格式
        String time = dateFormat.format(now);
        userDiary.setUserSignOutTime(time);
        if(studentService.stuLogoutRecord(userDiary)!=0){
            System.out.println("退出登录时间成功记录"+time);
        }

        session.removeAttribute("adminData");
        return "adminLogin";
    }

    //管理员查看管理员
    @RequestMapping("/admin/toViewAdmin")
    public String toViewAdmin(Model model){

        List<Admin> list=adminService.showAllAdmin();
        model.addAttribute("list",list);
        return "adminViewAdmin";
    }

    //通过管理员账号查找发布过的公告
    @RequestMapping("/admin/toFindAnnounce")
    public String toFindAnnounce(Admin admin,Model model){
        List<Announcement> list=adminService.findAnnounceByAdminName(admin);
        model.addAttribute("list",list);
        return "adminViewNotice";
    }

    //通过管理员账号查找注册过的老师
    @RequestMapping("/admin/toFindTeacher")
    public String toFindTeacher(Admin admin,Model model){
        List<Teacher> list=adminService.findTeacherByAdminName(admin);
        model.addAttribute("list",list);
        return "adminViewTeacher";
    }

    //管理员查看活动日志
    @RequestMapping("/admin/toViewDiary")
    public String toViewDiary(Admin admin,HttpSession session,Model model){
        List<UserDiary> list=adminService.adminViewDiary(admin);
        model.addAttribute("list",list);

        return "studentOperationDiary";
    }

}
